﻿using System;
using System.Collections.Generic;
using System.IO;
using Docentric.Examples.Data.BusinessObjects;
using Docentric.Examples.Data.BusinessObjects.DataModel;
using Docentric.Word;


namespace Docentric.Examples.Browser
{
	public class FormattingValuesExample : ExampleBase
	{
		// Constructor
		public FormattingValuesExample()
		{
			Name = "FormattingValues";
			Title = "Formatting Values";
			Description = "This example shows formatting capabilites of the toolkit. The template comprises of several 'Field' elements all bound to the default data source (.Net Object). You can notice that some of the elements have set format strings which are used at Report Generation Time to correctly format database values.";
			HelpTopicPage = "Examples_FormattingValues";
		}


		// GenerateReport
		public override string GenerateReport()
		{
			// Get the Order object with the Id = 3.
			Order order = DataAccess.GetOrderById(3);

			// Create a temporary file for the generated report document.
			string reportDocumentFileName = System.IO.Path.GetTempPath() + "GeneratedReport_" + Guid.NewGuid().ToString() + ".docx";
			using (Stream reportDocumentStream = File.Create(reportDocumentFileName))
			{
				// Open the report template file.
				using (Stream reportTemplateStream = GetReportTemplate())
				{
					// Generate the report document using 'DocumentGenerator'.
					DocumentGenerator dg = new DocumentGenerator(order);
					DocumentGenerationResult result = dg.GenerateDocument(reportTemplateStream, reportDocumentStream);
					if (result.HasErrors)
					{
						foreach (Word.Error error in result.Errors) Console.Out.WriteLine(error.Message);
					}
				}
			}

			return reportDocumentFileName;
		}
	}
}
